import pymongo ;
import os ;
import re ;
import datetime;
from pymongo import MongoClient;
import time
from PyQt4.QtCore import QDir,QObject,QThread
from KDataBase import KGlobals
from KDataBase.KDBUtility import KDBUtility
from KDataBase.KDBProvider import KEODProvider,KStooqProvider
from KDataBase.KAbstractDB import KAbstractDB 
import zipfile,shutil
class KUpdateDB(KAbstractDB):
	def __init__(self,parent=None):
		super(KUpdateDB,self).__init__(self)
	def ProcessUpdateFile(self,Path,Provider,DoUnZip=True):
		#Store The incremental file into internal data directory, and also update to mongodb database
		TempPath,other=Path.rsplit(r'/',1)
		TempPath+=r'/temp'	
		#DoUnZip=False # for debug

		#1: unzip the file
		if DoUnZip==True:
			if not zipfile.is_zipfile(Path):
				self.UpdateProgress("This is not a valid zip file.")
				return True
			else:
				self.UpdateProgress("Extracting Data.")
				with zipfile.ZipFile(Path, 'r') as myzip:
					if os.path.exists(TempPath):
						shutil.rmtree(TempPath,True)
					os.mkdir(TempPath)
					myzip.extractall(TempPath)
				self.UpdateProgress("Data Extracted.")	
			
		#2: parse the file
		if Provider=="EOD":
			self.provider=KEODProvider.KEODProvider(self,self.db)
		elif Provider=="Stooq":
			self.provider=KStooqProvider.KStooqProvider(self,self.db)   
		if self.provider:
			self.provider.ProgressUpdated.connect(self.ProgressUpdated)  
			##self.provider.UpdateHistory(r"D:/Dropbox/Trading2013/Data/ToUpdate/temp/data/5 min","5min")
			self.provider.UpdateAllHistory(TempPath+r'/data/5 min','5min')	
		return True
	
	##import history data from files of specified data provider   
	def BuildAllHistory(self,sfilepath,sprovider):
		if sprovider=="EOD":
			self.provider=KEODProvider.KEODProvider(self,self.db)
		elif sprovider=="Stooq":
			self.provider=KStooqProvider.KStooqProvider(self,self.db)   
		if self.provider:
			self.provider.ProgressUpdated.connect(self.ProgressUpdated)     
			return self.provider.BuildAllHistory(sfilepath)
	def DoWork(self): ##called by new thread
		if self.ThreadArgs['Op']=="Update":
			finished=self.ProcessUpdateFile(self.ThreadArgs['Path'],self.ThreadArgs['Provider'],True)
		if finished==False: ## interuptted by user
			self.UpdateProgress("Interuppted by User")











